package com.l024.framework.db;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

/**
 * 数据库帮助类
 */
public class LitePalHelper {
    private static LitePalHelper litePalHelper;
    private LitePalHelper(){

    }
    public static LitePalHelper getInstance(){
        if(litePalHelper==null){
            synchronized (LitePalHelper.class){
                if(litePalHelper==null){
                    litePalHelper = new LitePalHelper();
                }
            }
        }
        return litePalHelper;
    }

    /**
     * 保存的基类
     * @param support
     */
    public void baseSave(LitePalSupport support){
       support.save();
    }

    /**
     * 保存新朋友消息
     */
    public void savaNewFriend(String msg,String id){
        NewFriend newFriend = new NewFriend();
        newFriend.setMsg(msg);
        newFriend.setUserId(id);
        newFriend.setSavaTime(System.currentTimeMillis());
        newFriend.setIsAgree(-1);
        baseSave(newFriend);
    }

    /**
     * 查询的基类
     */
    public List<? extends LitePalSupport> baseQuery(Class cls){
        return LitePal.findAll(cls);
    }

    /**
     * 查询新朋友
     */
    public List<NewFriend> queryNewFriend(){
        return (List<NewFriend>)baseQuery(NewFriend.class);
    }

    /**
     * 更新新朋友状态
     */
    public void updateNewFriend(String id,int agree){
        NewFriend newFriend = new NewFriend();
        newFriend.setIsAgree(agree);
        newFriend.updateAll("userId=?",id);
    }
}
